﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Schema;
using BusinessLogic.Dto;
using Common;

namespace Services
{
    [ServiceBehavior(Namespace = "http://wgrus.com/Service", AddressFilterMode = AddressFilterMode.Any)]
    public class AccountingServices : IAccountingServices
    {
        public int GetNextOrderNumber()
        {
            return BusinessLogic.Facade.Instance.GetNextOrderNumber();
        }


        public StandingReport IsStandingValid(string orderXml)
        {
            bool error = false;
            string errorText = "";
            StandingReport res;

            res = new StandingReport
            {
                BadStandingReason = "Invalid document",
                CustomerId = -1,
                IsStandingValid = false
            };

            if (string.IsNullOrEmpty(orderXml))
            {
                return res;
            }

            try
            {
                //Validate XML velformed
                XDocument doc = XDocument.Parse(orderXml);

                //Schema validate XML
                doc.Validate(Constants.AccountingOrderSchema, (o, e) =>
                {
                    error = true;
                    errorText = e.Message;
                }, true);
                if (error)
                {
                    return res;
                }

                //Handle Order
                res = BusinessLogic.Facade.Instance.IsStandingValid(doc);
                return res;

            }
            catch (Exception e)
            {
                return res;
            }

        }



        public CustomerUpdateReport UpdateCustomer(string customerXml)
        {
            bool error = false;
            string errorText = "";

            CustomerUpdateReport res = new CustomerUpdateReport
            {
                CustomerId = -1,
                ErrorReason = "Invalid document",
                IsSuccesfull = false
            };


            if (string.IsNullOrEmpty(customerXml))
            {
                return res;
            }

            try
            {
                //Validate XML velformed
                XDocument doc = XDocument.Parse(customerXml);

                //Schema validate XML
                doc.Validate(Constants.AccountingCustomerSchema, (o, e) =>
                {
                    error = true;
                    errorText = e.Message;
                }, true);
                if (error)
                {
                    return res;
                }

                //Handle Order
                return BusinessLogic.Facade.Instance.UpdateCustomer(doc);

            }
            catch (Exception e)
            {
                return res;
                //throw new Exception(e.Message);
            }
        }


        public ShipOrderReport OrderShipped(string orderXml)
        {
            bool error = false;
            string errorText = "";

            ShipOrderReport res = new ShipOrderReport
            {
                OrderId = -1,
                ErrorReason = "Invalid document",
                IsSuccesfull = false
            };

            if (string.IsNullOrEmpty(orderXml))
            {
                return res;
            }

            try
            {
                //Validate XML velformed
                XDocument doc = XDocument.Parse(orderXml);

                //Schema validate XML
                doc.Validate(Constants.AccountingOrderSchema, (o, e) =>
                {
                    error = true;
                    errorText = e.Message;
                }, true);
                if (error)
                {
                    return res;
                }

                //Handle Order
                return BusinessLogic.Facade.Instance.OrderShipped(doc);

            }
            catch (Exception e)
            {
                return res;
                //throw new Exception(e.Message);
            }
        }
    }
}